package com.wyx.controller;

import com.wyx.pojo.TArticle;
import com.wyx.service.TArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private TArticleService tArticleService;

    @RequestMapping("/toIndex")
    public String toIndex(Model model){

        List<TArticle> tArticleList = tArticleService.findAll();

        for (TArticle tArticle : tArticleList){

            SimpleDateFormat dateformat = new SimpleDateFormat("yy-MM-dd");
            tArticle.setDate(dateformat.format(tArticle.getCreated()));

//            System.out.println(tArticle);

        }


        model.addAttribute("tArticleList", tArticleList);

        return "client/index";

    }

    @RequestMapping("/changePage")
    @ResponseBody
    public List<TArticle> changePage(@RequestParam Integer pageNum){

        List<TArticle> tArticleList = tArticleService.findByPage(pageNum);

        for (TArticle tArticle : tArticleList){
            System.out.println(tArticle);
        }

        return tArticleList;

    }

//    @RequestMapping("/toIndex")
//    public String list(){
//
//        return "client/index";
//
//    }


}
